package com.esd.crm.controller;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.servlet.ModelAndView;

import com.esd.common.util.UsernameAndPasswordMd5;
import com.esd.crm.Constants;
import com.esd.crm.entity.AreaCode;
import com.esd.crm.entity.User;
import com.esd.crm.service.AreaCodeService;
import com.esd.crm.service.UserService;
import com.esd.crm.util.KitService;
import com.esd.vo.PageUtil;
import com.esd.vo.ReturnResult;
import com.github.pagehelper.PageInfo;

@RestController
@RequestMapping("/user")
public class UserController {

	@Autowired
	private UserService userService;
	

	/**
	 * 跳转查询页
	 * @return
	 */
	@RequestMapping(value = "/list", method = RequestMethod.GET)
	public ModelAndView getUser() {
//		User use= userService.getUserInfo();
//		System.out.println(use.toString());
		return new ModelAndView("user/user-list");
	}
	/**
	 * 查询所有用户
	 * @param param
	 * @param page
	 * @return
	 */
	@RequestMapping(value = "/list",method = RequestMethod.POST)
	@ResponseBody
	public ReturnResult getAll(User param,PageUtil page) {
		PageInfo<User> pageInfo = userService.findByPage(param, page);
		return ReturnResult.storeData(pageInfo);
	}
	
	/**
	 * 通过id删除用户
	 * @param param
	 * @param page
	 * @return
	 */
	@RequestMapping(value = "/delete",method = RequestMethod.POST)
	@ResponseBody
	public Map<String,Object> delete(String id,PageUtil page) {
//		PageInfo<User> pageInfo = 
		boolean b = userService.deleteById(id);
		if(!b) {
			return ReturnResult.fail();
		}
		return ReturnResult.success();
	}
	
	/**
	 * 批量删除
	 * @param idArray
	 * @param reuqest
	 * @return
	 */
	@RequestMapping(value = "/deleteBatch" , method = RequestMethod.POST)
	@ResponseBody
	public Map<String,Object> deleteBatch(@RequestParam("idArray")String [] idArray,HttpServletRequest reuqest){
		userService.deleteBatch(idArray);
		return ReturnResult.success();
	}
	
	@RequestMapping(value = "/add", method = RequestMethod.GET)
	public ModelAndView add_get(HttpServletRequest request) {
		ReturnResult entity = ReturnResult.success();
		
		entity.put(Constants.HTML_PAGE_TYPE, Constants.HTML_PAGE_TYPE_NEW);
		return new ModelAndView("user/user-html", entity);
	}
	
	@RequestMapping(value = "/add", method = RequestMethod.POST)
	@ResponseBody
	public Map<String,Object> add_post(User user,HttpServletRequest request,HttpSession session) {
		//1. 校验用户名称是否已经存在
		User checkUser = userService.findOneByUserName(user.getUsername());
		if(checkUser != null) {
			return ReturnResult.fail("用户名称已存在.");
		}
		//2. 新增
		String userId = KitService.getUUID();
		user.setId(userId);
//		user.setUpdateUser(userId);
		//密码加密
		UsernameAndPasswordMd5 md5 = new UsernameAndPasswordMd5();
		user.setPassword(md5.getMd5(user.getUsername(), user.getPassword()));
		System.out.println("pwd====>"+user.getPassword());
		boolean b = userService.insertSelective(user);
		if(!b) {
			return ReturnResult.fail();
		}
		return ReturnResult.success();
	}

	
	//修改
	@RequestMapping(value = "/edit/{id}/{editable}", method = RequestMethod.GET)
	public ModelAndView edit_get(@PathVariable(value = "id") String id, @PathVariable(value = "editable") int editable,
			HttpServletRequest request) {
		ReturnResult entity = ReturnResult.success();
		System.out.println("editid=====>"+id);
		//1.获取用户信息
		User user = userService.getUserInfo(id);
		entity.put("user", user);
		
		entity.put(Constants.HTML_PAGE_TYPE, Constants.HTML_PAGE_TYPE_EDIT);
		return new ModelAndView("user/user-html", entity);
	}
		
	
	
	
	
}
